<template>
	<gui-page ref="guiPage" :customHeader="true">
		<!-- 导航 -->
		<template v-slot:gHeader>
			<nav-bar-custom left-icon="left" :title="$t('创建放货任务')" @clickLeftBack="clickLeftBack"
				scanType="0"  ></nav-bar-custom>
		</template>

		<template v-slot:gBody>
			<view class="barcodeSplitting">
				<!-- 扫描条码部分 -->
				<view class="gui-bg-white gui-dark-bg-level-3 gui-padding-x distributed-head">
					<view class="custom-card-title">
						<text class="title-line"></text>
						{{ $t('Common.scanningInfo') }}
					</view>
					<!-- <view class="custom-card-itemTwo" style="padding: 10rpx 0;">
						<text class="item-info-title" style="width:180rpx"><text class="required-color">*</text>{{
							$t('taskManagement.taskType')
						}}：</text>
						<view class="gui-form-body gui-text-right" style="overflow: visible;">
							<comboGrid ref="comboGridRef" v-model="data.taskType" :requestParam="data.requestParamType"
								requestUrl="/api1/iot/open/GetTaskType" :size="9999"
								:placeholder="$t('distributedEnter.placeholderTextChoose')" inputKey="itemValue"
								:column="data.columnRule.column" bindValue="locationName" @setClick="selectTypeClick">
							</comboGrid>
						</view>
					</view> -->
					<view class="custom-card-itemTwo" style="padding: 10rpx 0;">
						<text class="item-info-title"><text class="required-color">*</text>{{
							$t('taskManagement.priority')
						}}：</text>
						<view class="gui-form-body gui-text-right" style="overflow: visible;">
							<superweiCombox style="text-align: right;" :candidates="data.priorityList"
								:placeholder="$t('distributedEnter.placeholderTextChoose')" :isJSON="true"
								keyName="name" @select="priorityClick" v-model="data.priorityName">
							</superweiCombox>
						</view>
					</view>
					<view class="custom-card-itemTwo" style="padding: 10rpx 0;">
						<text class="item-info-title"><text class="required-color">*</text>{{ $t('mexico.scanType')
							}}：</text>
						<view class="gui-form-body gui-text-right" style="overflow: visible;">
							<superweiCombox style="text-align: right;" :candidates="data.scanList"
								:placeholder="$t('distributedEnter.placeholderTextChoose')" :isJSON="true"
								keyName="name" @select="scanTypeClick" v-model="data.scanType">
							</superweiCombox>
						</view>
					</view>
				</view>
				<view class="gui-flex gui-rows gui-nowrap gui-align-items-center gui-margin-top"
					style="margin-top:15rpx">
					<view class="gui-title-line"></view>
					<text class="gui-primary-text gui-h6" style="padding-left:50rpx; padding-right:50rpx;">{{
						$t('taskManagement.taskDetailsInfor') }}</text>
					<view class="gui-title-line"></view>
				</view>
				<scroll-view :show-scrollbar="false" :scroll-y="true" class="scroll" @scrolltolower="scrolltolowerFn">
					<view style="height: 15rpx"></view>
					
					<view class="custom-form-card" >
						<uni-collapse v-model="data.value">
							<uni-collapse-item titleBorder="none" :show-animation="true">
								<template v-slot:title>
									<view class="custom-card-title">
										<text class="title-line"></text>
										{{ $t('taskManagement.operationType' )}}: {{  $t('taskManagement.releaseGoods')}}
									</view>
								</template>
								<view class="input-padding custom-card-itemOne" style="padding:0 0 20rpx 0">
									<text class="item-info-title" style="width:140rpx">{{ $t('translationImage.process')
										}}：</text>
									<view class="input-dialog-item-count" style="width:100%;position: relative;">
										<comboGrid ref="comboGridRef" v-model="tableData.list[0].processCode"
											:requestParam="data.requestParam" requestUrl="/api1/iot/open/GetProcessData"
											:placeholder="$t('distributedEnter.placeholderTextChoose')" inputKey="code"
											:column="data.columnProcessCodeRule.column" bindValue="locationName"
											@setClick="getRuleClicks">
										</comboGrid>
									</view>
								</view>
								<view class="input-padding custom-card-itemOne">
									<text class="item-info-title" style="width:140rpx">{{
										$t('multilingual.productionLine')
									}}：</text>
									<view class="input-dialog-item-count" style="width:100%;position: relative;">
										<comboGrid ref="comboGridRef" v-model="tableData.list[0].lineCode" :size="9999"
											:requestParam="data.requestParam2"
											requestUrl="/api1/iot/open/GetProcessData"
											:placeholder="$t('distributedEnter.placeholderTextChoose')" inputKey="code"
											:column="data.columnLineCodeRule.column" bindValue="locationName"
											@setClick="getRuleClicks2">
										</comboGrid>
									</view>
								</view>
								<view class="input-padding custom-card-itemOne">
									<text class="item-info-title" style="width:140rpx">{{ $t('mexico.position')
										}}：</text>
									<view class="input-dialog-item-count" style="width:100%;position: relative;">
										<comboGrid ref="comboGridRef" v-model="tableData.list[0].locationCode" :size="9999"
											:requestParam="data.requestParam3"
											requestUrl="/api1/iot/open/GetProcessData"
											:placeholder="$t('distributedEnter.placeholderTextChoose')" inputKey="code"
											:column="data.columnLocationCodeRule.column" bindValue="locationName"
											@setClick="getRuleClicks3">
										</comboGrid>
									</view>
								</view>
								<view class="input-padding custom-card-itemOne" >
									<text class="item-info-title" style="width:200rpx">{{ $t('Common.pointPosition')
										}}：</text>
									<view class="input-dialog-item-count" style="width:100%">
										<uni-easyinput class="input-border" v-model="tableData.list[0].seq" 
											:placeholder="$t('taskManagement.pleaseEnter')" />
									</view>
								</view>
								<view class="input-padding custom-card-itemOne" style="padding:30rpx 0 ">
									<text class="item-info-title" style="width:200rpx">{{ $t('taskManagement.goodsType')
										}}：</text>
									<view class="input-dialog-item-count" style="width:100%">
										<uni-data-select v-model="tableData.list[0].wareType" :localdata="data.createTaskType" 
											@change="wareTypeClicks"
											style="text-align: left;width:100%;z-index:9999"></uni-data-select>
									</view>
								</view>
								<view class="input-padding custom-card-itemOne">
									<text class="item-info-title" style="width:200rpx">{{
										$t('taskManagement.operationType')
									}}：</text>
									<view class="input-dialog-item-count" style="width:100%">
										<uni-data-select v-model="tableData.list[0].operations" :localdata="data.operationRule" 
											@change="operationClicks"></uni-data-select>
									</view>
								</view>
								
								<view class="input-padding custom-card-itemOne">
									<text class="item-info-title" style="width:200rpx">{{ $t('distributedEnter.qty')
										}}：</text>
									<view class="input-dialog-item-count" style="width:100%">
										<uni-easyinput v-model="tableData.list[0].qty" type="number"
											:placeholder="$t('taskManagement.pleaseEnter')" />
									</view>
								</view>
								<view class="input-padding custom-card-itemOne">
									<text class="item-info-title" style="width:350rpx">{{
										$t('taskManagement.destinationsQty')
									}}：</text>
									<view class="input-dialog-item-count" style="width:100%">
										<uni-easyinput class="input-border" v-model="tableData.list[0].currentQty" type="number"
											:placeholder="$t('taskManagement.pleaseEnter')" />
									</view>
								</view>
								
								<view class="input-padding custom-card-itemOne" >
									<text class="item-info-title" style="width:200rpx">{{ $t('taskManagement.cartonNo')
										}}：</text>
									<view class="input-dialog-item-count" style="width:100%">
										<uni-easyinput class="input-border" v-model="tableData.list[0].cartonNo" 
											:placeholder="$t('taskManagement.pleaseEnter')" />
									</view>
								</view>
							</uni-collapse-item>
						</uni-collapse>
					</view>
				</scroll-view>
				<!-- 底部按钮 -->
				<view class="page-bottom-box bottom-postion">
					<view class="page-bottomBtn bottomBtn-bgcolor-green" @click="submit">
						{{ $t('calendar.submit') }}
					</view>
				</view>
			</view>
		</template>

	</gui-page>
</template>

<script setup>
import { navigate, navigateBack } from '@/utils/routerSkip.js';
import { reactive, ref, onMounted, computed, watch, toRaw, toRef } from 'vue';
import { getDataDictionaryFn, pitchOnFn, sortClass } from '@/utils/index.js'
import { createTask } from '@/api/api.js'
import superweiCombox from '@/components/superweiCombox';
import comboGrid from "@/components/comboGrid";
import tableColor from '@/hooks/tableColor.js'
import storageBin from '@/components/storageBin.vue'
import { useStore } from "vuex";
import { onBackPress } from '@dcloudio/uni-app';
import pdaScan from '@/hooks/pdaScan.js'
import { USERINFO } from '@/constant/index.js'

//国际化引入
import { useI18n } from 'vue-i18n';
import { on } from 'form-data';
const { t } = useI18n();

let store = useStore()
const { scan } = pdaScan()

components: {
	storageBin,
		superweiCombox,
		comboGrid
}
//let { tableData } = tableColor();
const { cellStyle, cellHeaderStyle } = tableColor();
let popup = ref(null)
let inputDialog = ref(null)

const tableData=reactive({
	list:[{"processCode":""},{"processCode":""}]
})

const data = reactive({
	value: ['0'],
	operationRule: [{
		text: t('taskManagement.releaseGoods'),
		value: 'DELIVERY'
	}, {
		text: t('taskManagement.deliveryGoods'),
		value: 'COLLECT'
	}],
	columnRule: {
		column: [{
			label: t('Common.pointPositionCode'),
			name: "itemText",
		}, {
			label: t('Common.pointPositionName'),
			name: "itemValue",
		}]
	},//任务类型
	taskType: '',
	taskTypeName: '',
	priorityList: [{
		name: '低',
		id: '低'
	}, {
		name: '较低',
		id: '较低'
	}, {
		name: '正常',
		id: '正常'
	}, {
		name: '较高',
		id: '较高'
	}, {
		name: '高',
		id: '高'
	}],
	priority: null,
	priorityName:'',
	scanList: [ {
		name: t('taskManagement.putScan'),
		id: 'PUT'
	}, {
		name: t('taskManagement.pickScan'),
		id: 'NO'
	}],//扫码类型
	scanType: 'PICK',
	scanTypeName: 'PICK',
	column1: [{
		name: 'age',
		type: 'ailibtn',
		label: t('translationImage.operation'),
		scanValue: '',
		fixed: true,
		width: 80,
	},
	// {
	// 	type: "index",
	// 	label: t('translationImage.serialNumber'),
	// 	width: 80,
	// },
	{
		name: 'processCode',
		label: t('translationImage.process'),
		width: 110,
		emptyString: '--'
	}, {
		name: 'lineCode',
		label: t('multilingual.productionLine'),
		width: 140,
	},
	{
		name: 'locationCode',
		label: t('mexico.position'),
		width: 140,
		emptyString: '--'
	}, {
		name: 'seq',
		label: t('Common.pointPosition'),
		scanValue: '',
		width: 140,
	},
	{
		name: 'operations',
		label: t('taskManagement.operationType'),
		width: 150,
		emptyString: '--'
	}, {
		name: 'wareType',
		label: t('taskManagement.goodsType'),
		width: 160,
		emptyString: '--'
	},
	{
		name: 'qty',
		label: t('distributedEnter.qty'),
		width: 80,
		emptyString: '--'
	},
	{
		name: 'currentQty',
		label: t('taskManagement.destinationsQty'),
		width: 140,
		emptyString: '--'
	},
	{
		name: 'cartonNo',
		label: t('taskManagement.cartonNo'),
		width: 140,
		emptyString: '--'
	}],
	requestParamType: {

	},
	requestParam: {
		type: '1',
		parentCode: '',
		scanType: ''
	},
	requestParam2: {
		type: '2',
		parentCode: '',
		scanType: ''
	},
	requestParam3: {
		type: '3',
		parentCode: '',
		scanType: '',
		ProcessCode: ''
	},
	columnProcessCodeRule: {
		column: [{
			label: t('translationImage.process'),
			name: "text",
		}, {
			label: 'code',
			name: "code",
		}]
	},
	columnLineCodeRule: {
		column: [{
			label: t('newTranslation.productionLineCode'),
			name: "text",
		}, {
			label: t('newTranslation.productionLineName'),
			name: "code",
		}]
	},
	columnLocationCodeRule: {
		column: [{
			label: t('Common.pointPositionCode'),
			name: "text",
		}, {
			label: t('Common.pointPositionName'),
			name: "code",
		}]
	},
	popupInfo: {

	}, //当前行的弹窗信息
	selectIndex: 0,// 选择表格的第几行数据进行编辑
	currCartonNo: '',//托盘号
	currOutputNo: '',// 产出编号
	createTaskType: [{
		text: 'WoodCarton60',
		value: 'WoodCarton60'
	}, {
		text: 'SteelCarton60',
		value: 'SteelCarton60'
	}, {
		text: 'SteelCarton48',
		value: 'SteelCarton48'
	}, {
		text: 'WoodCarton48',
		value: 'WoodCarton48'
	}],
})



onMounted(() => {
	let str = {
		attribute1: null,
		attribute2: null,
		attribute3: null,
		autoStart: 1,
		batchNo: null,
		carrierCode: null,
		code: "",
		createBy: "",
		createTime: "",
		createUserName: "",
		createUserNo: "",
		currentQty: null,
		delFlag: null,
		extra: null,
		id: null,
		isEffective: null,
		lastModifyTime: "",
		lastModifyUserName: "",
		lastModifyUserNo: "",
		lineCode: "",
		lineName: "",
		locationCode: "",
		locationName: "",
		materialCode: null,
		operation: "",
		percentvalue: null,
		processCode: "",
		processName: "",
		qty: 1,
		robotCode: null,
		robotType: null,
		seq: null,
		status: "",
		taskCode: "",
		taskId: null,
		tenantId: null,
		type: "",
		wareOrientation: null,
		wareType: "",
		cartonNo:""
	}
	tableData.list[0] = str
	tableData.list[0].operation = '放货'
	tableData.list[0].operations = 'DELIVERY'
})

// 任务类型 选择
const selectTypeClick = (e) => {
	console.log(e)
	data.taskType = e.itemValue
	data.taskTypeName = e.itemText
}
// 任务优先级 选择
const priorityClick = (e) => {
	data.priorityName = e.id
	switch(e.id)
	{
		
		case "低":
		data.priority = 1;
		break;
		case "较低":
			data.priority = 2;
		break;
		case "正常":
			data.priority = 3;
		break;
		
		case "较高":
			data.priority = 4;
		break;
		
		case "高":
			data.priority = 5;
		break;
	}
}
// 扫码类型 选择
const scanTypeClick = (e) => {
	data.scanType = e.name
	data.scanTypeName = e.id
}

// 第一个card的数据
// 操作类型 选择
const operationClick = (e) => {
	console.log(e)
	tableData.list[0].operations = e
}
// 货物类型选择 
const wareTypeClick = (e) => {
	tableData.list[0].wareType = e
	
}



//第二个card的数据
// 操作类型 选择
const operationClicks = (e) => {
	console.log(e)
	tableData.operations = e
}
// 货物类型选择 
const wareTypeClicks = (e) => {
	console.log(e)
	tableData.list[0].wareType = e
}
// 工序选择
const getRuleClicks = (e,index) => {
	console.log(e,index)
	tableData.list[0].processCode = e.code
	data.requestParam2.parentCode = e.code
	tableData.list[0].processName = e.text
}

// 产线选择
const getRuleClicks2 = (e) => {
	tableData.list[0].lineCode = e.code
	data.requestParam3.parentCode = e.code
	data.requestParam3.ProcessCode = tableData.list[0].processCode
	tableData.list[0].lineName = e.text
}
// 位置选择
const getRuleClicks3 = (e) => {
	tableData.list[0].locationCode = e.code
	tableData.list[0].locationName = e.text
	tableData.list[0].wareType = e.details[0].cartonType //货物类型
	tableData.list[0].seq = e.details[0].point || e.details[0].bigpoint  // 点位
	tableData.list[0].currentQty = e.details[0].qty //目的地存在数量
	tableData.list[0].currCMaterialName = e.details[0].currCMaterialName //物料名称

	tableData.list[0].currOutputNo = '' //产出编号
	tableData.list[0].currCartonNo = '' //托盘号
	tableData.list[0].currCMaterialCode = "" //物料编码
	if (e.details.length > 1) {
		e.details.forEach(item => {
			tableData.list[0].currCartonNo = tableData.list[0].currCartonNo + item.currCartonNo + ','
			tableData.list[0].currCMaterialCode = tableData.list[0].currCMaterialCode + item.currCMaterialCode + ','
			tableData.list[0].currOutputNo = tableData.list[0].currOutputNo + item.currOutputNo + ','
		})
	} else {
		tableData.list[0].currCartonNo = e.details[0].currCartonNo
		tableData.list[0].currOutputNo = e.details[0].currOutputNo
		tableData.list[0].currCMaterialCode = e.details[0].currCMaterialCode
	}
	data.currCartonNo = tableData.list[0].currCartonNo
	data.currOutputNo = tableData.list[0].currOutputNo
}

// 创建任务 提交
const submit = () => {
	// if (!data.taskType) {
	// 	uni.showToast({
	// 		title: t('taskManagement.taskTypeNotNull'),
	// 		icon: 'none'
	// 	})
	// 	return
	// }
	if (!data.priority) {
		uni.showToast({
			title: t('taskManagement.priorityNotNull'),
			icon: 'none'
		})
		return
	}
	// if (!data.scanType) {
	// 	uni.showToast({
	// 		title: t('mexico.scanTypePlease'),
	// 		icon: 'none'
	// 	})
	// 	return
	// }
	let list = tableData.list.filter(t => t.processCode.length == 0)
	if (list.length) {
		uni.showToast({
			title: t('taskManagement.processNotNull'),
			icon: 'none'
		})
		return
	}

	
	let userInfo = uni.getStorageSync(USERINFO); //获取当前登录人
	let parmes = {
		businessTaskType: data.taskType, //任务类型
		businessTaskTypeName: data.taskTypeName, //任务名称
		initPriority: data.priority,//优先级
		cartonNo: data.currCartonNo,//托盘号
		outputNo: data.currOutputNo,//产出编号
		materialCode: tableData.list[0].currCMaterialCode,//物料代码
		materialName: tableData.list[0].currCMaterialName,//物料名称
		scanType: data.scanTypeName,//扫描类型
		cartonType: tableData.list[0].wareType ,//货物类型
		createUserName: userInfo.name,//用户名称
		lastModifyUserName: userInfo.name,
		createBy: userInfo.username,//用户工号
		createUserNo: userInfo.username,
		lastModifyUserNo: userInfo.username,
		listSteps: []
	}
	let listSteps = []
	tableData.list.forEach(item => {
		listSteps.push({
			code: item.seq,
			processCode: item.processCode,
			lineCode: item.lineCode,
			locationCode: item.locationCode,
			seq: 0,
			operation: item.operations,
			wareType: item.wareType,
			qty: item.qty,
			currentQty: item.currentQty,
			processName: item.processName,
			lineName: item.lineName,
			locationName: item.locationName
		})
	})
	parmes.listSteps = listSteps
	createTask(parmes).then(res => {
		try {
			let inputParameter = JSON.stringify(parmes)
			let returnParameter = JSON.stringify(res)
			let operationPicture = 'AGV任务管理--创建任务'
			let callInterface = res.rquestUrl
			let opertionParmes = {
				inputParameter,
				returnParameter,
				operationPicture,
				callInterface,
				responseCode: res.code,//返回状态
				errorMessage: res.message,//错误信息
				isPass: res.code == 200 ? 'Y' : 'N',//是否成功 Y N
				operationType: OPERATION_COMMIT,
				module: 'WMS',
				source: "PDA",
			}
			basPdaOperationLogFn(opertionParmes)
		} catch (error) {
		}
		if (res.code == 200) {
			uni.showToast({ title: t('smashRemoval.submitSuccessfully'), icon: 'none' })
			uni.navigateBack({
				delta: 1
			});
		} else {
			uni.showToast({ title: res.message, icon: 'none' })
		}
	})
}


//导航栏左侧返回
onBackPress((options) => {
	if (options.from == 'backbutton') {
		navBackCheck()
		return true;
	}
})

const clickLeftBack = () => {
	navBackCheck()
}
const navBackCheck = () => {
	uni.navigateBack({
		delta: 1
	});
}

// 下拉加载
const scrolltolowerFn = () => {

}
</script>

<style lang="scss" scoped>
.barcodeSplitting {
	display: flex;
	flex-direction: column;
	height: calc(100vh - 88rpx);
	// height: 100vh;
}

.distributed-head {
	border-bottom-left-radius: 16rpx;
	border-bottom-right-radius: 16rpx;
}

.scroll {
	flex: 1;
	height: 2rpx;
	margin-bottom: 140rpx !important;

}

.input-padding {
	padding: 20rpx 0;
	border-bottom: 0.03125rem solid #F2F2F2 !important;
}

.custom-form-card{
	padding-bottom: 10rpx;
}
</style>

